<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserxinqingLogic
 *
 * @author Administrator
 */
class UserxinqingLogic extends BaseLogic{
    //put your code here
    private $userxinqingTable;
    public function __construct() {
        parent::__construct();
        $this->userxinqingTable=  $this->load("user_xinqing");
        $this->table=  $this->userxinqingTable;
    }
    /**
     *    保存和更新用户心情
     * @param type $data 用户心情信息
     * @param type $id   用户心情id
     * @return type  
     */
    public function save($data,$id=0){
        if($id>0){
            $res=  $this->userxinqingTable->update($data,"id={$id}");
        }else{
            $res=$this->userxinqingTable->insert($data,TRUE);
        }
        return $res;
    }
    
    /**
     * 获得心情列表
     * @param type $where 条件
     * @param type $pageid  页码
     * @param type $pagecount   页数
     * @param type $orderby   排序
     * @return type   列表
     */
    public function getList($where="1=1",$pageid=1,$pagecount=100,$orderby="order by create_time desc"){
       $data=array();
       $res=$this->userxinqingTable->select_page("*,FROM_UNIXTIME(create_time,'%d日 %H:%i:%s') as create_time2", $where, $pageid, $pagecount,$orderby);
       $count=  $this->userxinqingTable->get_count($where);
       $data["list"]=$res;
       $data["pagedata"]=array("data_count"=>$count,"page_count"=>  ceil($count/$pagecount),"cur_page"=>$pageid); 
       return $data;
    }
    /**
     * 获得用户心情概率列表
     * @return type
     */
    public function getProbList(){
        $data=  $this->userxinqingTable->execute("select a.*,a.prob/b.allcount as p,b.allcount as allcount from(select count(xinqing_id) as prob,user_id,xinqing_id,`month`,`day` from jp_user_xinqing GROUP BY user_id,xinqing_id,`month`,`day`)as a LEFT JOIN (SELECT COUNT(xinqing_id) as allcount,user_id,`month`,`day` from jp_user_xinqing GROUP BY user_id,`month`,`day`) as b on a.user_id=b.user_id and a.`month`=b.`month` and a.`day`=b.`day`");
        return $data;
    }
    public function delUserXinQing($user_xinqing_id=0){
        if($user_xinqing_id>0){
            $userxininfo=  $this->getOne("id={$user_xinqing_id}");
            $sql="update jp_user_xinqing set hum_temp_weight=hum_temp_weight+0.5,status=0,remark='用户删除心情' where id={$user_xinqing_id}";
            $this->userxinqingTable->query($sql);
            //历年来下条件=当前user_id，xinqing_id，month，day，temp，status=1的（hum_temp_weight=1=hum_temp_weight+0.5/查询出来的个数）
            $cond="user_id={$userxininfo["user_id"]} and xinqing_id={$userxininfo["xinqing_id"]} and month={$userxininfo["month"]} and day={$userxininfo["day"]} and temp={$userxininfo["temp"]} and status=1";
            $c=  $this->userxinqingTable->get_count($cond);
			$c2=$this->userxinqingTable->get_one("sum(hum_temp_weight) as hum_temp_weight",$cond);
            if($c>0){
                $update_sql="update jp_user_xinqing set hum_temp_weight=hum_temp_weight+(({$c}-{$c2["hum_temp_weight"]})/(2*{$c})) where {$cond}";
                $this->userxinqingTable->query($update_sql);
            }
            
        }
    }
   
}
